package com.whw.fy.service.impl;

import com.whw.fy.entity.*;
import com.whw.fy.mapper.*;
import com.whw.fy.service.TechnicalTypeService;
import com.whw.fy.utils.AgeUtil;
import com.whw.fy.utils.GetEveryDate;
import com.whw.fy.utils.ListUtil;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import com.whw.fy.service.PlansService;

import java.util.*;

@Service
public class PlansServiceImpl implements PlansService {

    @Resource
    private PlansMapper plansMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private ClazzMapper clazzMapper;
    @Resource
    private LessonMapper lessonMapper;
    @Resource
    private TechnicalTypeMapper technicalTypeMapper;

    @Override
    public Message deleteByPrimaryKey(Integer plansId) {
        return plansMapper.deleteByPrimaryKey(plansId) > 0 ? Message.success(null) : Message.fail(null);
    }


    @Override
    public Message insertSelective(Plans record) {
        return plansMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer plansId) {
        return Message.success(null).add(plansMapper.selectByPrimaryKey(plansId));
    }

    @Override
    public Message updateByPrimaryKeySelective(Plans record) {
        return plansMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }


    @Override
    public Message selectAll() {
        List<Plans> plansList = plansMapper.selectAll();
        return Message.success(null).add(initPlansList(plansList));
    }

    @Override
    public Message selectByClassId(int classId) {
        String studentIds = clazzMapper.selectByPrimaryKey(classId).getStudentId();
        String[] studentIdss = studentIds.split(";");
        List<Map> plansList = new LinkedList<>();
        if (studentIdss.length > 0) {
            for (String id : studentIdss) {
                List<Plans> plansList1 = plansMapper.selectAllByPlansStudentId(Integer.parseInt(id));
                if (plansList1.size() > 0) {
                    for (Plans plans : plansList1) {
                        HashMap map = new HashMap();
                        if (plans.getPlansStudentId() != null)
                            map.put("plansStudentName", studentMapper.selectByPrimaryKey(plans.getPlansStudentId()).getStudentName());
                        else map.put("plansStudentName", null);
                        if (plans.getPlansPeopleTeacherId() != null)
                            map.put("PlansPeopleTeacherId", staffMapper.selectByPrimaryKey(plans.getPlansPeopleTeacherId()).getStaffName());
                        else map.put("PlansPeopleTeacherId", null);
                        if (plans.getPlansPeopleResponsibleTeacherId() != null)
                            map.put("PlansPeopleResponsibleTeacherId", staffMapper.selectByPrimaryKey(plans.getPlansPeopleResponsibleTeacherId()).getStaffName());
                        else map.put("PlansPeopleResponsibleTeacherId", null);
                        if (plans.getPlansTeacherId() != null)
                            map.put("plansTeacherName", staffMapper.selectByPrimaryKey(plans.getPlansTeacherId()).getStaffName());
                        else map.put("plansTeacherName", null);
                        map.put("plans", plans);
                        plansList.add(map);
                    }
                }

            }
        }
        return Message.success(null).add(plansList);
    }

    @Override
    public Message selectByPlansStudentId(Integer plansStudentId) {
        List<Plans> plansList = plansMapper.selectAllByPlansStudentId(plansStudentId);
        List<Map> mapList = new LinkedList<>();
        for (Plans plans : plansList) {
            HashMap map = new HashMap();
            if (plans.getPlansStudentId() != null)
                map.put("plansStudentName", studentMapper.selectByPrimaryKey(plans.getPlansStudentId()).getStudentName());
            else map.put("plansStudentName", null);
            if (plans.getPlansPeopleTeacherId() != null)
                map.put("PlansPeopleTeacherId", staffMapper.selectByPrimaryKey(plans.getPlansPeopleTeacherId()).getStaffName());
            else map.put("PlansPeopleTeacherId", null);
            if (plans.getPlansPeopleResponsibleTeacherId() != null)
                map.put("PlansPeopleResponsibleTeacherId", staffMapper.selectByPrimaryKey(plans.getPlansPeopleResponsibleTeacherId()).getStaffName());
            else map.put("PlansPeopleResponsibleTeacherId", null);
            if (plans.getPlansTeacherId() != null)
                map.put("plansTeacherName", staffMapper.selectByPrimaryKey(plans.getPlansTeacherId()).getStaffName());
            else map.put("plansTeacherName", null);
            map.put("plans", plans);
            mapList.add(map);
        }
        return Message.success(null).add(mapList);
    }

    @Override
    public int batchInsert(List<Plans> list) {
        return plansMapper.batchInsert(list);
    }


    @Override
    public int insert(Plans record) {
        return plansMapper.insert(record);
    }

    @Override
    public int updateByPrimaryKey(Plans record) {
        return plansMapper.updateByPrimaryKey(record);
    }



    @Override
    public int updateBatchSelective(List<Plans> list) {
        return plansMapper.updateBatchSelective(list);
    }

    @Override
    public Message selectallByTeacherIdJurisdictionEight(int staffId) {
        List<Clazz> clazzList = clazzMapper.selectByClassTeacherId(staffId);
        List<Integer> clazzIdList = new LinkedList<>();
        for (Clazz clazz : clazzList) {
            clazzIdList.add(clazz.getClassId());
        }
        clazzIdList = ListUtil.duplicate(clazzIdList);
        List<Plans> plansList = new ArrayList<>();
        for (Integer id : clazzIdList) {
            List<Plans> plansList1 = plansMapper.selectAllByPlansClazzId(id);
            plansList.addAll(plansList1);
        }
        return Message.success(null).add(initPlansList(plansList));
    }

    @Override
    public Message selectallByMedicalDirectorIdJurisdictionZero(int staffId) {
        List<Student> studentList = studentMapper.selectByStudentTreatMedicalDirectorId(staffId);
        List<Plans> plansList = new ArrayList<>();
        for (Student student : studentList) {
            List<Plans> plansList1 = plansMapper.selectAllByPlansStudentId(student.getStudentId());
            plansList.addAll(plansList1);
        }
        return Message.success(null).add(initPlansList(plansList));
    }

    @Override
    public Message selectallByTeachingDirectorJurisdictionSix(int staffId) {
        List<Student> studentList = studentMapper.selectByStudentTreatTeachingDirectorId(staffId);

        List<Plans> plansList = new ArrayList<>();
        for (Student student : studentList) {
            List<Plans> plansList1 = plansMapper.selectAllByPlansStudentId(student.getStudentId());
            plansList.addAll(plansList1);
        }
        return Message.success(null).add(initPlansList(plansList));
    }

    @Override
    public Message selectallByprincipalTeacherJurisdictionSeven(int staffId) {
        List<Plans> plansList = plansMapper.selectByPlansPeopleResponsibleTeacherId(staffId);
        return Message.success(null).add(initPlansList(plansList));
    }


    private List<Map> initPlansList(List<Plans> plansList) {
        List<Map> mapList = new LinkedList<>();
        Date nowdate = new Date();
        for (Plans plans : plansList) {
            HashMap map = new HashMap();
            if (plans.getPlansStudentId() != null) {
                Student student = studentMapper.selectByPrimaryKey(plans.getPlansStudentId());
                if (student.getStudentBirth() != null) {
                    student.setStudentAge(AgeUtil.getAgeByBirth(student.getStudentBirth()));
                } else {
                    student.setStudentAge(-1);
                }
                //根据学生加入中心日期动态设置学生加入时长
                student.setStudentJoinTime(GetEveryDate.GetTimeBetween(student.getStudentJoinCenter(), nowdate));

                map.put("plansStudent", student);
            } else map.put("plansStudentName", null);
            if (plans.getPlansPeopleTeacherId() != null)
                map.put("PlansPeopleTeacherId", staffMapper.selectByPrimaryKey(plans.getPlansPeopleTeacherId()).getStaffName());
            else map.put("PlansPeopleTeacherId", null);
            if (plans.getPlansPeopleResponsibleTeacherId() != null)
                map.put("PlansPeopleResponsibleTeacherId", staffMapper.selectByPrimaryKey(plans.getPlansPeopleResponsibleTeacherId()).getStaffName());
            else map.put("PlansPeopleResponsibleTeacherId", null);
            if (plans.getPlansTeacherId() != null)
                map.put("plansTeacherName", staffMapper.selectByPrimaryKey(plans.getPlansTeacherId()).getStaffName());
            else map.put("plansTeacherName", null);
            if (plans.getPlansClazzId() != null) {
                Clazz clazz = clazzMapper.selectByPrimaryKey(plans.getPlansClazzId());
                Lesson lesson = lessonMapper.selectByPrimaryKey(clazz.getClassCourseId());
                map.put("technicalTypeName", lesson.getTechnicalType().getTechnicalTypeName());
                map.put("clazz", clazz);
            } else map.put("clazz", null);
            map.put("plans", plans);
            mapList.add(map);
        }
        return mapList;
    }
}







